<template>
    <div>
        <global-tables
            @changePage="handleChangePage"
            :total="total"
            @handleAllSelect="handleAllSelect"
            :list="list"
            :loading="loading"
            :columns="columns"
            :current="search.pageindex"
        >
            <Form :model="search" :label-width="80">
                <div class="table-top-wrap">
                    <div>
                        <div>
                            <Button v-if="isAddBtn" class="features-btn" @click="handleBtnClick(null,'add')">
                                <Icon type="md-add" size="18"/>新建
                            </Button>
                            <Button v-if="isProhibit" class="features-btn btn-auto-m" @click="handleBtnClick(null,'delete')">
                                <Icon type="ios-lock-outline" size="17"/>停用
                            </Button>
                            <Button v-if="isProhibit" class="features-btn btn-auto-m" @click="handleBtnClick(null,'open')">
                                <Icon type="ios-unlock-outline" size="17"/>启用
                            </Button>
                        </div>
                    </div>
                    <div class="table-top-wrap-bottom">
                        <div>
                            <Select v-model="search.type" style="width:100px; margin:0 10px" placeholder="门店类型">
                                <Option value="-1">全部</Option>
                                <Option value="0">自营</Option>
                                <Option value="1">加盟</Option>
                            </Select>
                        </div>
                        <div>
                            <Select v-model="search.status" style="width:80px; margin:0 10px" placeholder="状态">
                                <Option value="-1">全部</Option>
                                <Option value="0">关闭</Option>
                                <Option value="1">正常</Option>
                            </Select>
                        </div>
                        <div>
                            <input type="text" class="style-input" @keyup.enter="handleSearch" v-model="search.keyword" placeholder="请输入门店">
                            <i-input style="display:none"></i-input>
                        </div>
                        <div>
                            <Button icon="ios-search" @click="handleSearch" class="search-btn">搜索</Button>
                        </div>
                    </div>
                </div>
            </Form>
        </global-tables>
        <Modal :mask-closable="false" v-model="isModal" :title="ModalTitle" width="600">
            <div v-if="isModal">
                <Form ref="formItem" :model="formItem" :rules="ruleValidate" :label-width="100">
                    <Row>
                        <i-col :lg="{ span: 12}">
                            <FormItem label="门店名称" prop="ShopName">
                                <i-input v-model="formItem.ShopName" placeholder="请输入门店名称"></i-input>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 12}">
                            <FormItem label="门店类型" prop="ShopType">
                                <RadioGroup v-model="formItem.ShopType">
                                    <Radio label="0">自营</Radio>
                                    <Radio label="1">加盟</Radio>
                                </RadioGroup>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 12}">
                            <FormItem label="联系人" prop="Contact">
                                <i-input v-model="formItem.Contact" placeholder="请输入联系人"></i-input>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 12}">
                            <FormItem label="联系人电话" prop="Mobile">
                                <i-input v-model="formItem.Mobile" placeholder="请输入联系人电话"></i-input>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 12}">
                            <FormItem label="办公电话" prop="Tel">
                                <i-input v-model="formItem.Tel" placeholder="请输入办公电话"></i-input>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 24}">
                            <FormItem label="门店地址" prop="Address">
                                <i-input type="textarea" v-model="formItem.Address" placeholder="请输入门店地址"></i-input>
                            </FormItem>
                        </i-col>
                        <i-col :lg="{ span: 24}">
                            <FormItem label="备注" prop="Remark">
                                <i-input type="textarea" v-model="formItem.Remark" placeholder="请输入备注"></i-input>
                            </FormItem>
                        </i-col>
                    </Row>
                </Form>
            </div>
            <div slot="footer">
                <Button size="large" @click="handleClose">取消</Button>
                <Button type="primary" size="large" @click="handleSubmit('formItem')">确定</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
/**
 * 门店列表
 */
import { cyclicRouting, getdate } from '@/util';
import { getModulePermission } from '@/api/user';
import { getTablesList, addTablesItem, getItemInfo, setDisableEnable } from './service';
import GlobalTables from '@/components/GlobalTables';
export default {
    name: 'OrganizationShop',
    components: {
        GlobalTables
    },
    data() {
        return {
            allSelectArr: null,
            isAddBtn: false,
            isProhibit: false,
            isEdit: false,
            isModal: false,
            ModalTitle: '新建门店',
            itemId: 0,
            loading: true,
            formItem: this.initFormItem(),
            ruleValidate: this.initRuleValidate(),
            total: 0,
            list: [],
            search: {
                type: '',
                status: '',
                keyword: '',
                pageindex: 1,
                pagesize: 10
            },
            columns: [
                {
                    type: 'selection',
                    width: 60,
                    align: 'center'
                },
                {
                    title: '门店名称',
                    key: 'ShopName'
                },
                {
                    title: '门店编号',
                    width: 100,
                    key: 'ShopCode'
                },
                {
                    title: '门店类型',
                    key: 'ShopType',
                    width: 100,
                    render: (h, params) => {
                        const { ShopType } = params.row;
                        if (ShopType == 0) {
                            return h('div', [
                                h('Badge', {
                                    props: {
                                        status: 'success',
                                        text: '自营'
                                    }
                                })
                            ]);
                        } else if (ShopType == 1) {
                            return h('div', [
                                h('Badge', {
                                    props: {
                                        status: 'processing',
                                        text: '加盟'
                                    }
                                })
                            ]);
                        }
                    }
                },
                {
                    title: '联系人',
                    key: 'Contact',
                    width: 90
                },
                {
                    title: '联系电话',
                    width: 140,
                    key: 'Mobile'
                },
                {
                    title: '办公电话',
                    width: 140,
                    key: 'Tel'
                },
                {
                    title: '公司地址',
                    render: (h, params) => {
                        return h('div', [
                            h(
                                'Poptip',
                                {
                                    props: { placement: 'bottom' }
                                },
                                [
                                    h(
                                        'span',
                                        {
                                            style: {
                                                display: 'inline-block',
                                                width: params.column._width * 0.6 + 'px',
                                                overflow: 'hidden',
                                                textOverflow: 'ellipsis',
                                                whiteSpace: 'nowrap'
                                            }
                                        },
                                        params.row.Address
                                    ),
                                    h(
                                        'span',
                                        {
                                            slot: 'content',
                                            style: {
                                                whiteSpace: 'normal',
                                                wordBreak: 'break-all'
                                            }
                                        },
                                        params.row.Address
                                    )
                                ]
                            )
                        ]);
                    }
                },
                {
                    title: '状态',
                    key: 'IsActivity',
                    width: 80,
                    render: (h, params) => {
                        const { IsActivity } = params.row;
                        if (IsActivity == 0) {
                            return h('div', [
                                h('Badge', {
                                    props: {
                                        status: 'success',
                                        text: '关闭'
                                    }
                                })
                            ]);
                        } else if (IsActivity == 1) {
                            return h('div', [
                                h('Badge', {
                                    props: {
                                        status: 'processing',
                                        text: '正常'
                                    }
                                })
                            ]);
                        }
                    }
                },
                {
                    title: '创建日期',
                    key: 'CreateTime',
                    width: 140,
                    render: (h, params) => {
                        return h('div', [h('span', {}, getdate(new Date(params.row.CreateTime).getTime()))]);
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    width: 70,
                    render: (h, params) => {
                        const { isEdit } = this;
                        return h('div', [
                            isEdit &&
                                h(
                                    'Button',
                                    {
                                        props: {
                                            type: 'primary',
                                            size: 'small'
                                        },
                                        style: {
                                            background: '#5ab1ef',
                                            border: 'none',
                                            marginRight: '5px'
                                        },
                                        on: {
                                            click: () => {
                                                this.handleBtnClick(params.row, 'edit');
                                            }
                                        }
                                    },
                                    '修改'
                                )
                        ]);
                    }
                }
            ]
        };
    },
    mounted() {
        /* 获取菜单的id */
        cyclicRouting(JSON.parse(sessionStorage.getItem('user')).menuList, this.$route, id => {
            /* 按钮权限请求 */
            getModulePermission({
                roleid: JSON.parse(sessionStorage.getItem('user')).roleid,
                moduleid: id
            }).then(res => {
                /* 按钮权限赋值 */
                for (let item in res.data) {
                    if (res.data[item].name == 'add') {
                        this.isAddBtn = true;
                    } else if (res.data[item].name == 'onoff') {
                        this.isProhibit = true;
                    } else if (res.data[item].name == 'edit') {
                        this.isEdit = true;
                    }
                }
                /* 列表请求 */
                this.getTablesListData();
            });
        });
    },
    methods: {
        /* 表格搜索 */
        handleSearch() {
            this.loading = true;
            this.getTablesListData();
        },
        /* 关闭对话框 */
        handleClose() {
            this.isModal = false;
        },
        /* 复选框选中 */
        handleAllSelect(list) {
            let arr = [];
            for (let item in list) {
                arr.push(list[item].ID);
                arr.join(',');
            }
            arr = arr.join(',');
            this.allSelectArr = arr;
        },
        /* 提交 */
        handleSubmit(name) {
            const data = { ...this.formItem, ID: this.itemId };
            this.$refs[name].validate(valid => {
                if (valid) {
                    addTablesItem(data).then(res => {
                        if (res.state == 'success') {
                            this.$Message.success('操作成功');
                            this.loading = true;
                            this.isModal = false;
                            this.getTablesListData();
                        } else {
                            this.$Message.error(res.message);
                        }
                    });
                }
            });
        },
        /* 表格数据请求 */
        getTablesListData() {
            const data = this.search;
            getTablesList(data).then(res => {
                this.list = res.data.rows;
                this.loading = false;
                this.total = res.records;
            });
        },
        /* 表格分页方法 */
        handleChangePage(type, page) {
            this.loading = true;
            if (type == 'page') {
                this.search.pageindex = page;
                this.getTablesListData();
            } else {
                this.search.pagesize = page;
                this.getTablesListData();
            }
        },
        /* 对话框 */
        handleBtnClick(item, type) {
            this.ModalTitle = '新建门店';
            this.formItem = this.initFormItem();
            if (type == 'edit' || type == 'add') {
                this.itemId = 0;
                this.isModal = true;
                if (type == 'edit') {
                    this.ModalTitle = '修改门店';
                    getItemInfo({
                        id: item.ID
                    }).then(res => {
                        this.itemId = item.ID;
                        this.formItem = {
                            ...this.formItem,
                            ShopName: res.data.ShopName,
                            ShopType: res.data.ShopType,
                            Contact: res.data.Contact,
                            Mobile: res.data.Mobile,
                            Tel: res.data.Tel,
                            Address: res.data.Address,
                            Remark: res.data.Remark
                        };
                    });
                }
            } else if (type === 'delete' || type === 'open') {
                let title = '';
                if (type === 'delete') {
                    title = '停用';
                } else {
                    title = '启用';
                }
                if (this.allSelectArr) {
                    this.$Modal.confirm({
                        title: `你确定要${title}吗`,
                        onOk: () => {
                            setDisableEnable({
                                ids: this.allSelectArr,
                                operation: type === 'delete' ? false : true
                            }).then(res => {
                                if (res.state == 'success') {
                                    this.$Message.success(res.message);
                                    this.allSelectArr = '';
                                } else {
                                    this.$Message.error(res.message);
                                }
                                this.getTablesListData();
                            });
                        },
                        onCancel: () => {
                            this.$Message.info('取消成功');
                        }
                    });
                } else {
                    this.$Message.error('请先选择需要操作的记录');
                }
            }
        },
        /* 表单数据 */
        initFormItem() {
            return {
                ShopName: '',
                ShopType: '0',
                Contact: '',
                Mobile: '',
                Tel: '',
                Address: '',
                Remark: ''
            };
        },
        /* 表单校验 */
        initRuleValidate() {
            return {
                ShopName: [
                    {
                        required: true,
                        message: '请输入门店名称',
                        trigger: 'change'
                    }
                ],
                ShopType: [
                    {
                        required: true,
                        message: '请选择门店类型',
                        trigger: 'change'
                    }
                ],
                Contact: [
                    {
                        required: true,
                        message: '请输入门店联系人',
                        trigger: 'change'
                    }
                ],
                Mobile: [
                    {
                        required: true,
                        message: '请输入门店联系人电话',
                        trigger: 'change'
                    }
                ],
                Tel: [
                    {
                        required: false,
                        message: '请选输入公司名称',
                        trigger: 'change'
                    }
                ],
                Address: [
                    {
                        required: true,
                        message: '请输入门店地址',
                        trigger: 'change'
                    }
                ]
            };
        }
    }
};
</script>

<style>
</style>
